package com.Action;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.springframework.beans.factory.annotation.Autowired;

import transformFactory.CreateLinechart;

import com.Entity.Userhealthdata;
import com.Service.UserHealthDataService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HealthdataChartAction extends ActionSupport {

	private static final long serialVersionUID = 5752180822913527064L;

	private static final Logger logger = Logger
			.getLogger(HealthdataChartAction.class);

	@Autowired
	private UserHealthDataService userHealthDataService;
	private List<Userhealthdata> userhealthdatalist = null;

	private int userid;
	private String charttype;

	private JFreeChart chart;

	public void LineChartView() throws Exception {
		userid = Integer.parseInt(ServletActionContext.getRequest()
				.getParameter("userid"));
		charttype = new String(ServletActionContext.getRequest().getParameter(
				"charttype"));
		userhealthdatalist = this.userHealthDataService.findByUserId(userid);
		double[][] data = new double[1][userhealthdatalist.size()];

		String[] columnKeys = new String[userhealthdatalist.size()];

		String[] rowKeys = { charttype };
		if (charttype.equals("BloodSugar")) {
			for (int i = 0; i < userhealthdatalist.size(); i++) {
				data[0][i] = (double) userhealthdatalist.get(i).getBloodSugar();
				columnKeys[i] = userhealthdatalist.get(i).getDataUploadTime()
						.toString();
			}
		} else if (charttype.equals("DiastolicPressure")) {
			for (int i = 0; i < userhealthdatalist.size(); i++) {
				data[0][i] = (double) userhealthdatalist.get(i)
						.getDiastolicPressure();
				columnKeys[i] = userhealthdatalist.get(i).getDataUploadTime()
						.toString();
			}
		} else if (charttype.equals("SystolicPressure")) {
			for (int i = 0; i < userhealthdatalist.size(); i++) {
				data[0][i] = (double) userhealthdatalist.get(i)
						.getSystolicPressure();
				columnKeys[i] = userhealthdatalist.get(i).getDataUploadTime()
						.toString();
			}
		} else if (charttype.equals("UriaProtein")) {
			for (int i = 0; i < userhealthdatalist.size(); i++) {
				data[0][i] = (double) userhealthdatalist.get(i)
						.getUriaProtein();
				columnKeys[i] = userhealthdatalist.get(i).getDataUploadTime()
						.toString();
			}
		} else if (charttype.equals("Weight")) {
			for (int i = 0; i < userhealthdatalist.size(); i++) {
				data[0][i] = (double) userhealthdatalist.get(i).getWeight();
				columnKeys[i] = userhealthdatalist.get(i).getDataUploadTime()
						.toString();
			}
		}
		try {
			CreateLinechart demo = new CreateLinechart();
			HttpServletResponse response = (HttpServletResponse) ActionContext
					.getContext().get(
							org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
			response.setContentType("image/jpg");
			response.setCharacterEncoding("utf-8");
			chart = demo.createTimeXYChar(charttype + "折线图数据分析", "日期",
					charttype, demo.getBarData(data, rowKeys, columnKeys),
					"lineAndShap.jpg");
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
					500, 400, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
